﻿using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace Iworld.Backstage
{
    public partial class CreateAdministratorWindow : ChildWindow
    {
        public CreateAdministratorWindow()
        {
            InitializeComponent();
            this.Loaded += CreateAdministratorWindow_Loaded;
        }

        ObservableCollection<FilterBoxModel> groups = new ObservableCollection<FilterBoxModel>();

        void CreateAdministratorWindow_Loaded(object sender, RoutedEventArgs e)
        {
            AdministratorServiceClient client = new AdministratorServiceClient();
            client.GetAdministratorGroupsCompleted += client_GetAdministratorGroupsCompleted;
            client.GetAdministratorGroupsAsync(new GetAdministratorGroupsImport()
            {
                PageIndex = 1,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        
        }

        void client_GetAdministratorGroupsCompleted(object sender, GetAdministratorGroupsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                foreach (var k in e.Result.List)
                {
                    groups.Add(new FilterBoxModel(k.Name, k.Id, null));
                }
                UIGroup.ItemsSource = groups;
            }
            else
            {
                groups.Add(new FilterBoxModel("加载用户组失败....", null, null));
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enter(object sender, RoutedEventArgs e)
        {
            if (UIPassword.Password != UIPasswordComfirm.Password)
            {
                new ErrorPrompt() { Info = "两次密码输入不一致" }.Show();
                return;
            }
            FilterBoxModel fb = UIGroup.SelectedItem as FilterBoxModel;
            if (fb == null || fb.Value == null)
            {
                new ErrorPrompt() { Info = "请选择管理员组" }.Show();
                return;
            }
            AdministratorServiceClient client = new AdministratorServiceClient();
            client.CreateAdministratorCompleted += client_CreateAdministratorCompleted;
            client.CreateAdministratorAsync(new CreateAdministratorImport() {
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token,
                 Account=UIAccount.Text, 
                 Password=UIPassword.Password, 
                 Username=UIUsername.Text,
                 GroupId = (int)fb.Value
            });
        }

        void client_CreateAdministratorCompleted(object sender, CreateAdministratorCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                new ErrorPrompt() { Info = "添加管理员成功"}.Show();
                this.DialogResult = true;
            }
            else
            {
                new ErrorPrompt() { Info = e.Result.Error}.Show();
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
